package com.caigou.modules.famous.viewmodel

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.utils.Logger
import com.caigou.modules.famous.bean.ChallengeParamsItemBean
import com.caigou.modules.famous.bean.MyChallengeBean
import com.caigou.modules.famous.bean.MyChallengeItemBean
import com.caigou.modules.famous.repository.FamousRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/12/11
 */
@HiltViewModel
class ChallengeViewModel @Inject constructor(private val repository: FamousRepository) : ViewModel() {

    val myChallengeListLiveData = MutableLiveData<Resource<MyChallengeBean>>()
    val rewardRecordListLiveData = MutableLiveData<Resource<MutableList<MyChallengeItemBean>>>()
    val challengeParamsLiveData = MutableLiveData<Resource<MutableList<ChallengeParamsItemBean>>>()
    val joinChallengeLiveData = MutableLiveData<Resource<String>>()
    val cancelChallengeLiveData = MutableLiveData<Resource<String>>()

    fun getMyChallengeList(dareTypeId: Int, type: Int, userId: String) {
        myChallengeListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getMyChallengeList(dareTypeId, type, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<MyChallengeBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    myChallengeListLiveData.value = it
                }
        }
    }

    fun getRewardRecordList(type: Int) {
        rewardRecordListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getRewardRecordList(type)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<MyChallengeItemBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    rewardRecordListLiveData.value = it
                }
        }
    }

    fun getChallengeParamsList(dareTypeId: Int) {
        challengeParamsLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getChallengeParamsList(dareTypeId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<ChallengeParamsItemBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    challengeParamsLiveData.value = it
                }
        }
    }

    fun joinChallenge(dareTypeId: Int, type: Int, successDesc: String, moneyId: Int, userId: String) {
        joinChallengeLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.joinChallenge(dareTypeId, type, successDesc, moneyId, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.msg)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<String>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    joinChallengeLiveData.value = it
                }
        }
    }

    fun cancelChallenge(challengeRecordId: Int) {
        cancelChallengeLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.cancelChallenge(challengeRecordId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.msg)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<String>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    cancelChallengeLiveData.value = it
                }
        }
    }
}